package com.ctbri.vSearch.frame;

import com.ctbri.vSearch.util.PropUtil;
import org.apache.log4j.Logger;
import org.elasticsearch.client.transport.TransportClient;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.InetSocketTransportAddress;
import org.elasticsearch.xpack.client.PreBuiltXPackTransportClient;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.net.InetAddress;
import java.util.List;
import java.util.Vector;

/**
 * ES数据连接池
 * 
 * @author Hogan
 * 
 */
//@Component
//@Scope("singleton")
@Deprecated
public class ESDataPooledConnection {
	/*
	private static ESDataPooledConnection pooledConnection = new ESDataPooledConnection();
	private ESDataPooledConnection(){}
	public static ESDataPooledConnection newInstance(){
		return pooledConnection;
	}
	private Logger log = Logger.getLogger(getClass());
	*//**
	 * 连接数据元实体
	 *//*
	private ESDataBean eSDataBean;
	*//**
	 * 记录创建的总的连接数
	 *//*
	private int contActive;
	*//**
	 * 空闲连接队列
	 *//*
	private List<TransportClient> freeConnection = new Vector<TransportClient>();
	*//**
	 * 活动连接队列
	 *//*
	private List<TransportClient> activeConnection = new Vector<TransportClient>();
	*//**
	 * 将线程和连接绑定，保证事务能统一执行
	 *//*
	private static ThreadLocal<TransportClient> threadLocal = new ThreadLocal<TransportClient>();
	
	*//**
	 * 初始化  
	 *//*
    public void init() { 
    	this.eSDataBean = loadESDataBean();
    	for (int i = 0; i < eSDataBean.getInitConnections(); i++) {  
    		TransportClient conn = newConnection();
            // 初始化最小连接数  
            if (conn != null) {  
                freeConnection.add(conn);  
                contActive++;  
            }  
        }  
    	log.info("初始化ES连接数为："+ contActive);
    }  
    
    *//**
     * 获取当前连接
     * @return
     *//*
    public TransportClient getCurrentConnecton(){  
        // 默认线程里面取  
    	TransportClient conn = threadLocal.get();  
        if(conn == null){  
            conn = getConnection();  
        }  
        return conn;  
    }
    
    *//**
     * 获得连接 
     * @return
     *//*
    public synchronized TransportClient getConnection() {  
    	TransportClient conn = null;  
        try {  
            // 判断是否超过最大连接数限制  
            if(contActive < eSDataBean.getMaxActiveConnections()){  
                if (freeConnection.size() > 0) {  
                    conn = freeConnection.get(0);  
                    if (conn != null) {  
                        threadLocal.set(conn);  
                    }  
                    freeConnection.remove(0);  
                } else {  
                    conn = newConnection();  
                }  
			} else { 
                // 继续获得连接,直到从新获得连接  
                wait(this.eSDataBean.getConnTimeOut());  
                conn = getConnection();  
            }  
            if (conn != null) {  
                activeConnection.add(conn);  
                contActive ++;  
            }  
        } catch (Exception e) {  
            log.error("获取连接失败!",e);
            conn = null;
        } 
        return conn;  
    }  
    
	*//**
	 * 获得新连接
	 * 
	 * @return
	 *//*
	@SuppressWarnings({ "resource" })
	private synchronized TransportClient newConnection() {
		TransportClient client = null;
		System.out.println("获得了新连接");
		Settings settings = Settings.builder()
				.put("cluster.name", eSDataBean.getCluserName())
				.put("xpack.security.user", eSDataBean.getUsername())
				.build();
		try {
			client = new PreBuiltXPackTransportClient(settings)
					.addTransportAddress(new InetSocketTransportAddress(
							InetAddress.getByName(eSDataBean.getIp()),eSDataBean.getPort()));
		} catch (Exception e) {
			log.error("获取连接失败!",e);
			client = null;
		}
		return client;
	}

	*//**
	 * 释放连接
	 * 
	 * @param conn
	 *//*
    public synchronized void releaseConn(TransportClient conn)  {  
        if (conn != null && freeConnection.size() <= eSDataBean.getMaxConnections()) {  
            freeConnection.add(conn);  
            activeConnection.remove(conn);  
            contActive --;  
            threadLocal.remove();  
            // 唤醒所有正待等待的线程，去抢连接  
            notifyAll();  
        }  
    } 
    
   *//**
    * 销毁连接池  
    *//*
	public synchronized void destroy() {
		for (TransportClient conn : freeConnection) {
			if (conn != null) {
				conn.close();
			}
		}
		for (TransportClient conn : activeConnection) {
			if (conn != null) {
				conn.close();
			}
		}
		contActive = 0;
	}
	
	*//**
	 * 加载ES连接对象
	 * 
	 * @return
	 *//*
	public ESDataBean loadESDataBean(){
		ESDataBean eSDataBean= new ESDataBean();
		eSDataBean.setCluserName(PropUtil.getValue("es.cluster.name"));
		eSDataBean.setUsername(PropUtil.getValue("es.xpack.security.user"));
		eSDataBean.setIp(PropUtil.getValue("es.ip"));
		eSDataBean.setPort(Integer.valueOf(PropUtil.getValue("es.port")));
		eSDataBean.setPoolName(PropUtil.getValue("es.poolName"));
		eSDataBean.setMinConnections(Integer.valueOf(PropUtil.getValue("es.minConnections")));
		eSDataBean.setMaxConnections(Integer.valueOf(PropUtil.getValue("es.maxConnections")));
		eSDataBean.setInitConnections(Integer.valueOf(PropUtil.getValue("es.initConnections")));
		eSDataBean.setConnTimeOut(Long.valueOf(PropUtil.getValue("es.connTimeOut")));
		eSDataBean.setMaxActiveConnections(Integer.valueOf(PropUtil.getValue("es.maxActiveConnections")));
		eSDataBean.setConnectionTimeOut(Integer.valueOf(PropUtil.getValue("es.connectionTimeOut")));
		return eSDataBean;
	}
	
	*//**
	 * 连接数据元实体
	 * 
	 * @author Hogan
	 * 
	 *//*
	public static class ESDataBean {
		
		*//**
		 * 集群名称
		 *//*
		private String cluserName;
		*//**
		 * 用户名
		 *//*
		private String username;
		*//**
		 * 连接的IP地址
		 *//*
		private String ip;
		*//**
		 * 连接的IP端口
		 *//*
		private int port;
		*//**
		 * 连接池名称
		 *//*
		private String poolName;
		*//**
		 * 最小连接数
		 *//*
		private int minConnections;
		*//**
		 * 最大连接数
		 *//*
		private int maxConnections;
		*//**
		 * 初始化连接数
		 *//*
		private int initConnections;
		*//**
		 * 重复获得连接的频率
		 *//*
		private long connTimeOut;
		*//**
		 * 最大允许的连接数，和数据库对应
		 *//*
		private int maxActiveConnections;
		*//**
		 * 连接超时时间
		 *//*
		private long connectionTimeOut;

		public String getCluserName() {
			return cluserName;
		}

		public void setCluserName(String cluserName) {
			this.cluserName = cluserName;
		}

		public String getUsername() {
			return username;
		}

		public void setUsername(String username) {
			this.username = username;
		}

		public String getIp() {
			return ip;
		}

		public void setIp(String ip) {
			this.ip = ip;
		}

		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}

		public String getPoolName() {
			return poolName;
		}

		public void setPoolName(String poolName) {
			this.poolName = poolName;
		}

		public int getMinConnections() {
			return minConnections;
		}

		public void setMinConnections(int minConnections) {
			this.minConnections = minConnections;
		}

		public int getMaxConnections() {
			return maxConnections;
		}

		public void setMaxConnections(int maxConnections) {
			this.maxConnections = maxConnections;
		}

		public int getInitConnections() {
			return initConnections;
		}

		public void setInitConnections(int initConnections) {
			this.initConnections = initConnections;
		}

		public long getConnTimeOut() {
			return connTimeOut;
		}

		public void setConnTimeOut(long connTimeOut) {
			this.connTimeOut = connTimeOut;
		}

		public int getMaxActiveConnections() {
			return maxActiveConnections;
		}

		public void setMaxActiveConnections(int maxActiveConnections) {
			this.maxActiveConnections = maxActiveConnections;
		}

		public long getConnectionTimeOut() {
			return connectionTimeOut;
		}

		public void setConnectionTimeOut(long connectionTimeOut) {
			this.connectionTimeOut = connectionTimeOut;
		}
	}
*/
}
